package com.woniuxy.yygh.hsop.controller.api;

import com.woniuxy.yygh.common.exception.YyghException;
import com.woniuxy.yygh.common.helper.HttpRequestHelper;
import com.woniuxy.yygh.common.result.Result;
import com.woniuxy.yygh.common.result.ResultCodeEnum;
import com.woniuxy.yygh.common.utils.MD5;
import com.woniuxy.yygh.hsop.service.DepartmentService;
import com.woniuxy.yygh.hsop.service.HospitalService;
import com.woniuxy.yygh.hsop.service.HospitalSetService;
import com.woniuxy.yygh.hsop.service.ScheduleService;
import com.woniuxy.yygh.model.hosp.Department;
import com.woniuxy.yygh.model.hosp.Hospital;
import com.woniuxy.yygh.model.hosp.Schedule;
import com.woniuxy.yygh.vo.hosp.DepartmentQueryVo;
import com.woniuxy.yygh.vo.hosp.ScheduleQueryVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.util.StringUtils;

import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

/**
 * <P> @Author: ideaAdmin
 * <P> TODO: Class
 * <P> TODO: 医院上传数据的controller 传的都是封装好的json 然后发到mongdb
 *
 *      直接连接到管理微服务 hospital-manage
 * <P> Date: 2022/3/1:18:10
 * <P> Description:
 */
@RestController
@RequestMapping("/api/hosp")
public class ApiController {

    @Autowired
    private HospitalService hospitalService;

    @Autowired
    private HospitalSetService hospitalSetService;

    @Autowired
    private DepartmentService departmentService;

    @Autowired
    private ScheduleService scheduleService;


    //删除排班信息
    @PostMapping("/schedule/remove")
    public Result remove(HttpServletRequest request) {
        // 获取传递的医院信息
        Map<String, String[]> requestParameterMap = request.getParameterMap();

        //把string转为object
        Map<String, Object> parameterMap = HttpRequestHelper.switchMap(requestParameterMap);

        //医院编号
        String hoscode = (String) parameterMap.get("hoscode");

        //排班编号
        String hosScheduleId =
                (String) parameterMap.get("hosScheduleId");

        scheduleService.remove(hoscode, hosScheduleId);
        return Result.ok();
    }

    /**
     * TODO: 查询排班信息
     **/
    @PostMapping("/schedule/list")
    public Result findSchedule(HttpServletRequest request) {
        // 获取传递的医院信息
        Map<String, String[]> requestParameterMap = request.getParameterMap();

        //把string转为object
        Map<String, Object> parameterMap = HttpRequestHelper.switchMap(requestParameterMap);

        //医院编号
        String hoscode = (String) parameterMap.get("hoscode");

        //科室编号
        String depcode = (String) parameterMap.get("depcode");

        //获取页码 如果为空 等于1
        Integer page = StringUtils.isEmpty(parameterMap.get("page"))
                ? 1 : Integer.parseInt((String) parameterMap.get("page"));
        Integer limit = StringUtils.isEmpty(parameterMap.get("limit"))
                ? 1 : Integer.parseInt((String) parameterMap.get("limit"));

        ScheduleQueryVo scheduleQueryVo = new ScheduleQueryVo();
        scheduleQueryVo.setHoscode(hoscode);
        scheduleQueryVo.setDepcode(depcode);

        Page<Schedule> pages = scheduleService.findPageSchedule(page, limit, scheduleQueryVo);
        return Result.ok(pages);
    }

    /**
     * TODO:上传排班接口
     **/
    @PostMapping("/saveSchedule")
    public Result savaSchedule(HttpServletRequest request) {
        // 获取传递的医院信息
        Map<String, String[]> requestParameterMap = request.getParameterMap();

        //把string转为object
        Map<String, Object> parameterMap = HttpRequestHelper.switchMap(requestParameterMap);

        scheduleService.save(parameterMap);
        return Result.ok();
    }

    /**
     * TODO:删除科室接口
     **/
    @PostMapping("/department/remove")
    public Result removeDepartment(HttpServletRequest request) {
        // 获取传递的医院信息
        Map<String, String[]> requestParameterMap = request.getParameterMap();

        //把string转为object
        Map<String, Object> parameterMap = HttpRequestHelper.switchMap(requestParameterMap);

        String hoscode = (String) parameterMap.get("hoscode");
        String decode = (String) parameterMap.get("decode");

        departmentService.remove(hoscode, decode);

        return Result.ok();
    }

    /**
     * TODO:查询医院科室接口
     **/
    @PostMapping("/department/list")
    public Result findDepartment(HttpServletRequest request) {
        // 获取传递的医院信息
        Map<String, String[]> requestParameterMap = request.getParameterMap();

        //把string转为object
        Map<String, Object> parameterMap = HttpRequestHelper.switchMap(requestParameterMap);

        String hoscode = (String) parameterMap.get("hoscode");

        //获取页码 如果为空 等于1
        Integer page = StringUtils.isEmpty(parameterMap.get("page"))
                ? 1 : Integer.parseInt((String) parameterMap.get("page"));
        Integer limit = StringUtils.isEmpty(parameterMap.get("limit"))
                ? 1 : Integer.parseInt((String) parameterMap.get("limit"));

        DepartmentQueryVo departmentQueryVo = new DepartmentQueryVo();
        departmentQueryVo.setHoscode(hoscode);

        Page<Department> pages = departmentService.findPageDepartment(page, limit, departmentQueryVo);
        return Result.ok(pages);
    }

    /**
     * TODO:上传医院科室接口
     **/
    @PostMapping("/saveDepartment")
    public Result saveDepartment(HttpServletRequest request) {
        // 获取传递的医院信息
        Map<String, String[]> requestParameterMap = request.getParameterMap();

        //把string转为object
        Map<String, Object> parameterMap = HttpRequestHelper.switchMap(requestParameterMap);


        //医院编号
        //根据传递医院编码 查询数据库 查询签名
        String hospSign = (String) parameterMap.get("sign");
        String hoscode = (String) parameterMap.get("hoscode");

        String singKey = hospitalSetService.getSingKey(hoscode);

        //查询签名加密
        String singKeyMD5 = MD5.encrypt(singKey);

        //判断签名
        if (!hospSign.equals(singKeyMD5)) {
            throw new YyghException(ResultCodeEnum.SIGN_ERROR);
        }

        departmentService.save(parameterMap);

        return Result.ok();
    }


    /**
     * TODO: 查询医院的接口
     **/
    @PostMapping("/hospital/show")
    public Result getHospital(HttpServletRequest request) {
        // 获取传递的医院信息
        Map<String, String[]> requestParameterMap = request.getParameterMap();

        //把string转为object
        Map<String, Object> parameterMap = HttpRequestHelper.switchMap(requestParameterMap);


        //医院编号
        //根据传递医院编码 查询数据库 查询签名
        String hospSign = (String) parameterMap.get("sign");
        String hoscode = (String) parameterMap.get("hoscode");

        String singKey = hospitalSetService.getSingKey(hoscode);

        //查询签名加密
        String singKeyMD5 = MD5.encrypt(singKey);


        //根据医院编号查询
        Hospital hospital = hospitalService.getByHoscode(hoscode);
        return Result.ok(hospital);
    }


    //上传医院接口
    @PostMapping("/saveHospital")
    public Result saveHosp(HttpServletRequest request) {
        // 获取传递的医院信息
        Map<String, String[]> requestParameterMap = request.getParameterMap();

        //把string转为object
        Map<String, Object> parameterMap = HttpRequestHelper.switchMap(requestParameterMap);
        System.out.println(parameterMap.get("sign"));
        //获取医院系统传递过来的签名
        String hospSign = (String) parameterMap.get("sign");

        //根据传递医院编码 查询数据库 查询签名
        String hoscode = (String) parameterMap.get("hoscode");

        String singKey = hospitalSetService.getSingKey(hoscode);

        //查询签名加密
        String singKeyMD5 = MD5.encrypt(singKey);


        String logoData = (String) parameterMap.get("logoData");
        // System.out.println(logoData);
        logoData = logoData.replaceAll(" ", "+");
        System.out.println("**********-----------------------");
        //System.out.println(logoData);

        parameterMap.put("logoData", logoData);
        // System.out.println(parameterMap);
        hospitalService.save(parameterMap);
        return Result.ok();
    }


}
